package com.wangzhixuan.controller;

import javax.validation.Valid;

import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wangzhixuan.commons.result.PageInfo;
import com.wangzhixuan.commons.utils.StringUtils;
import com.wangzhixuan.model.TTag;
import com.wangzhixuan.service.ITTagService;
import com.wangzhixuan.commons.base.BaseController;

/**
 * <p>
 * 组织机构 前端控制器
 * </p>
 *
 * @author zhixuan.wang
 * @since 2017-07-04
 */
@Controller
@RequestMapping("/tTag")
public class TTagController extends BaseController {

    @Autowired private ITTagService tTagService;
    
    @GetMapping("/manager")
    public String manager() {
        return "admin/tTag/tTagList";
    }
    
    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(TTag tTag, Integer page, Integer rows, String sort,String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        EntityWrapper<TTag> ew = new EntityWrapper<TTag>(tTag);
        if(StringUtils.isNotBlank(tTag.getName())){
	        ew.like("`name`", tTag.getName());
	        tTag.setName(null);
	    }
        Page<TTag> pages = getPage(pageInfo);
        pages = tTagService.selectPage(pages, ew);
        pageInfo.setRows(pages.getRecords());
        pageInfo.setTotal(pages.getTotal());
        return pageInfo;
    }
    
    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/tTag/tTagAdd";
    }
    
    /**
     * 添加
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(@Valid TTag tTag) {
    	tTag.setCreateTime(new Date());
        boolean b = tTagService.insert(tTag);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }
    
    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        TTag tTag = new TTag();
        tTag.setId(id);
        boolean b = tTagService.updateById(tTag);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }
    
    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        TTag tTag = tTagService.selectById(id);
        model.addAttribute("tTag", tTag);
        return "admin/tTag/tTagEdit";
    }
    
    /**
     * 编辑
     * @param 
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Object edit(@Valid TTag tTag) {
        boolean b = tTagService.updateById(tTag);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }
}
